//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<cached_file.h>
///	@path	~/src/database/store/
///	@date	2007/12/04
///	@desc	Cached file accessing.

#pragma once

#include "xeres.h"

#include "database/store/IFile.h"

namespace xeres {

	/*!
		\class	CachedFile
		\brief	
	*/
	class CachedFile
	{
	public:

		/// \ctor
		CachedFile( RefWeak<IFile> file , size_t buffer );

		/// \dtor
		~CachedFile( void );

	//@ local

		/*!
			\brief		Move to next data.
		*/
		bool Next( ushort step_in = 1 );

		/*!
			\brief		Move back to previous data.
		*/
		bool Prev( ushort step_back = 1 );

		/*!
			\brief		Get current data.
		*/
		const byte * Current( void ) const;

		/*!
			\brief		Flush current cache & step in next.
		*/
		size_t Flush( uchar rollback = 0 );

		/*!
			\brief		Determine if file cache meets EOF.
		*/
		bool eof( void ) const;

		/*!
			\brief		Get current character.
		*/
		byte operator * ( void ) const;

		/*!
			\brief		Get character by index.
		*/
		byte operator [] ( uchar index );

		/*!
			\brief		Move pointer forward.
		*/
		void operator += ( size_t count );

		/*!
			\brief		Move pointer forward.
		*/
		inline void operator ++ ( void );

	private:

	//@ data
		Ref<IFile>		m_file;
		byte *			m_buffer;
		size_t			m_current;
		size_t			m_buffered;
	};

} // namespace xeres

#include "database/store/cached_file.inl"
